$(function () {

    let storage = window.localStorage;
    let applyNo = storage.getItem("applyNo");

    let postData = {};
    if (applyNo != null && applyNo != "" && applyNo != 'null') {
        postData['applyNo'] = applyNo;

    }
    storage.setItem("applyNo", null);

    $("#jqGrid").Grid({
        postData: postData,
        url: '../order/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '客户', name: 'customerName', width: 80},
            {label: '银行', name: 'bankName', index: 'promo_user_id', width: 80},
            {label: '产品名称', name: 'productName', width: 80},
            {label: '贷款年限', name: 'loanYears', index: 'loan_years', width: 80},
            {
                label: '贷款额度',
                name: 'loanQuota',
                index: 'loan_quota',
                width: 80,
                formatter: function (cellValue, options, cell) {
                    if (null == cellValue || cellValue == "") {
                        return "";
                    }

                    return cellValue + " 万元";
                }
            },
            {label: '客户经理', name: 'promoUserName', width: 80},
            {label: '贷款提交时间', name: 'loanSubmitAt', index: 'loan_submit_at', width: 80},
            {
                label: '进度',
                name: 'orderStatus',
                index: 'order_status',
                width: 80,
                formatter: function (cellValue, options, cell) {
                    //订单状态  10:订单已提交   20:贷款申请中   30:贷款成功   40:贷款失败
                    if (cellValue == 10) {
                        return "订单已提交";
                    }

                    if (cellValue == 20) {
                        return "贷款申请中";
                    }

                    if (cellValue == 30) {
                        return "贷款成功";
                    }

                    if (cellValue == 40) {
                        return "贷款失败";
                    }

                    return "未知状态:" + cellValue;
                }
            },
            {label: '最新跟进时间', name: 'loanLastFollowAt', index: 'loan_last_follow_at', width: 80},
            {
                label: '操作', name: 'operation', width: 120, formatter: function (cellValue, options, cell) {
                    return "<a data-id='" + cell['id'] + "' data-applyno='" + cell["applyNo"] + "' class='detail-link'>查看订单</a>&nbsp;&nbsp;";
                }
            }
        ]
    });


});
$(document)
    .off("click", ".detail-link")
    .on("click", ".detail-link", function () {
        let id = $(this).data("id");
        details(id);
        vm.show.updateDiv = false;
        vm.show.orderDetails = true;
        vm.show.freeDetail = true;
        vm.show.chargeWay = true;
        vm.show.progressInformation = true;
        vm.title = "贷款订单详情";
    });

function buildOrderInfo() {
    return {
        "applyNo": null,
        "applyType": null,
        "promoUserId": null,
        "productId": null,
        "loanQuota": null,
        "loanYears": null,
        "loanRate": null,
        "orderStatus": null,
        "loanLastFollowAt": null,
        "loanSubmitAt": null,
        "loanApplyAt": null,
        "loanSuccessAt": null,
        "loanFailAt": null,
        "customerName": null,
        "customerMobile": null,
        "productName": null,
        "bankName": null,
        "orderContract": {
            "applyNo": null,
            "serviceFee": null,
            "guaranteeFee": null,
            "accountManagementFee": null,
            "riskGuaranteeFee": null,
            "channelAgentFee": null,
            "invoiceRate": null,
            "informationFee": null,
            "advanceFee": null,
            "prepaymentFee": null,
            "mortgageRegistrationQueryFee": null,
            "mortgageEmergencyFee": null,
            "receiveAdvanceMoney": null,
            "contractUrl": null
        }
    }
}

let vm = new Vue({
    el: '#rrapp',
    data: {
        show: {
            orderDetails: false,
            freeDetail: false,
            chargeWay: false
        },
        showList: true,
        title: null,
        order: buildOrderInfo(),
        ruleValidate: {},
        q: {
            customerName: '',
            productName: ''
        },
        options: {}
    },
    mounted: function () {
        let vmObj = this;
        Ajax.request({
            url: "../common/getOptionValues",
            type: "GET",
            contentType: "application/json",
            async: true,
            successCallback: function (r) {
                vmObj.options = r;
                vmObj.options['applyTypeArr'] = [
                    {value: "1", label: "个人贷款"},
                    {value: "2", label: "企业贷款"},
                ];
            }
        });

        // methods里面定义的方法, 需要赋值给window
        window.details = this.details;
    },
    methods: {
        details: function (id) {
            vm.showList = false;
            vm.getInfo(id);
        },
        query: function () {
            vm.reload();
        },
        downloadContract: function () {
            let a = document.createElement('a')
            a.href = vm.order.orderContract.contractUrl;
            a.click()
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.show.updateDiv = true;
            vm.show.orderDetails = false;
            vm.show.freeDetail = false;
            vm.show.chargeWay = false;
            vm.show.progressInformation = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.order.id == null ? "../order/save" : "../order/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.order),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../order/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../order/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.order = r.order;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'customerName': vm.q.customerName,
                    'productName': vm.q.productName,
                },
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                customerName: '',
                productName: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});